<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI旅游助手 - 厦门旅游</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', 'SF Pro Display', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #fff;
            overflow-x: hidden;
        }

        .ai-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .ai-header {
            text-align: center;
            margin-bottom: 3rem;
            position: relative;
        }

        .ai-title {
            font-size: 3rem;
            font-weight: 700;
            background: linear-gradient(45deg, #00f5ff, #ff00ff, #00ff00);
            background-size: 200% 200%;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            animation: gradientShift 3s ease-in-out infinite;
            margin-bottom: 1rem;
        }

        @keyframes gradientShift {
            0%, 100% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
        }

        .ai-subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
            margin-bottom: 2rem;
        }

        .back-nav {
            position: absolute;
            top: 0;
            left: 0;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            padding: 0.8rem 1.5rem;
            border-radius: 25px;
            text-decoration: none;
            color: #fff;
            font-weight: 500;
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .back-nav:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: translateY(-2px);
        }

        .chat-container {
            flex: 1;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            border: 1px solid rgba(255, 255, 255, 0.2);
            display: flex;
            flex-direction: column;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
        }

        .chat-header {
            background: linear-gradient(135deg, rgba(0, 245, 255, 0.3), rgba(255, 0, 255, 0.3));
            padding: 1.5rem;
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .ai-avatar {
            width: 50px;
            height: 50px;
            background: linear-gradient(45deg, #00f5ff, #ff00ff);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.1); }
        }

        .ai-status {
            flex: 1;
        }

        .ai-name {
            font-size: 1.3rem;
            font-weight: 600;
            margin-bottom: 0.3rem;
        }

        .ai-description {
            font-size: 0.9rem;
            opacity: 0.8;
        }

        .chat-messages {
            flex: 1;
            padding: 2rem;
            overflow-y: auto;
            max-height: 500px;
            scrollbar-width: thin;
            scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
        }

        .chat-messages::-webkit-scrollbar {
            width: 6px;
        }

        .chat-messages::-webkit-scrollbar-track {
            background: transparent;
        }

        .chat-messages::-webkit-scrollbar-thumb {
            background: rgba(255, 255, 255, 0.3);
            border-radius: 3px;
        }

        .message {
            margin-bottom: 1.5rem;
            display: flex;
            align-items: flex-start;
            gap: 1rem;
            animation: messageSlide 0.5s ease-out;
        }

        @keyframes messageSlide {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .message.user {
            flex-direction: row-reverse;
        }

        .message-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.2rem;
            flex-shrink: 0;
        }

        .user .message-avatar {
            background: linear-gradient(45deg, #ff6b6b, #feca57);
        }

        .ai .message-avatar {
            background: linear-gradient(45deg, #00f5ff, #ff00ff);
        }

        .message-content {
            background: rgba(255, 255, 255, 0.1);
            padding: 1rem 1.5rem;
            border-radius: 20px;
            max-width: 70%;
            border: 1px solid rgba(255, 255, 255, 0.2);
            backdrop-filter: blur(10px);
        }

        .user .message-content {
            background: linear-gradient(135deg, rgba(255, 107, 107, 0.3), rgba(254, 202, 87, 0.3));
        }

        .ai .message-content {
            background: linear-gradient(135deg, rgba(0, 245, 255, 0.2), rgba(255, 0, 255, 0.2));
        }

        .chat-input-container {
            padding: 2rem;
            border-top: 1px solid rgba(255, 255, 255, 0.2);
            background: rgba(255, 255, 255, 0.05);
        }

        .chat-input-wrapper {
            display: flex;
            gap: 1rem;
            align-items: flex-end;
        }

        .chat-input {
            flex: 1;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.3);
            border-radius: 25px;
            padding: 1rem 1.5rem;
            color: #fff;
            font-size: 1rem;
            resize: none;
            min-height: 50px;
            max-height: 120px;
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }

        .chat-input:focus {
            outline: none;
            border-color: #00f5ff;
            box-shadow: 0 0 20px rgba(0, 245, 255, 0.3);
        }

        .chat-input::placeholder {
            color: rgba(255, 255, 255, 0.6);
        }

        .send-button {
            background: linear-gradient(45deg, #00f5ff, #ff00ff);
            border: none;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 1.2rem;
            color: #fff;
        }

        .send-button:hover {
            transform: scale(1.1);
            box-shadow: 0 10px 20px rgba(0, 245, 255, 0.4);
        }

        .send-button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
        }

        .loading {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            color: rgba(255, 255, 255, 0.8);
        }

        .loading-dots {
            display: flex;
            gap: 0.3rem;
        }

        .loading-dot {
            width: 8px;
            height: 8px;
            background: #00f5ff;
            border-radius: 50%;
            animation: loadingBounce 1.4s infinite ease-in-out;
        }

        .loading-dot:nth-child(1) { animation-delay: -0.32s; }
        .loading-dot:nth-child(2) { animation-delay: -0.16s; }

        @keyframes loadingBounce {
            0%, 80%, 100% {
                transform: scale(0);
            }
            40% {
                transform: scale(1);
            }
        }

        .welcome-message {
            text-align: center;
            padding: 3rem 2rem;
            opacity: 0.8;
        }

        .welcome-title {
            font-size: 1.5rem;
            margin-bottom: 1rem;
            color: #00f5ff;
        }

        .welcome-suggestions {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 1rem;
            margin-top: 2rem;
        }

        .suggestion-card {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 15px;
            padding: 1.5rem;
            cursor: pointer;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
        }

        .suggestion-card:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
        }

        .suggestion-icon {
            font-size: 2rem;
            margin-bottom: 1rem;
        }

        .suggestion-title {
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: #00f5ff;
        }

        .suggestion-text {
            font-size: 0.9rem;
            opacity: 0.8;
        }

        .error-message {
            background: rgba(255, 107, 107, 0.2);
            border: 1px solid rgba(255, 107, 107, 0.5);
            border-radius: 10px;
            padding: 1rem;
            margin: 1rem 0;
            color: #ff6b6b;
        }

        @media (max-width: 768px) {
            .ai-container {
                padding: 1rem;
            }
            
            .ai-title {
                font-size: 2rem;
            }
            
            .message-content {
                max-width: 85%;
            }
            
            .welcome-suggestions {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="ai-container">
        <div class="ai-header">
            <a href="index.html" class="back-nav">← 返回首页</a>
            <h1 class="ai-title">🤖 AI旅游助手</h1>
            <p class="ai-subtitle">基于DeepSeek AI的智能旅游问答系统</p>
        </div>

        <div class="chat-container">
            <div class="chat-header">
                <div class="ai-avatar">🤖</div>
                <div class="ai-status">
                    <div class="ai-name">厦门旅游AI助手</div>
                    <div class="ai-description">为您提供专业的厦门旅游咨询服务</div>
                </div>
            </div>

            <div class="chat-messages" id="chatMessages">
                <div class="welcome-message">
                    <h3 class="welcome-title">👋 欢迎使用AI旅游助手！</h3>
                    <p>我是您的专属厦门旅游顾问，可以为您解答关于厦门旅游的各种问题。</p>
                    
                    <div class="welcome-suggestions">
                        <div class="suggestion-card" onclick="sendSuggestion('厦门有哪些必去的景点？')">
                            <div class="suggestion-icon">🏛️</div>
                            <div class="suggestion-title">热门景点</div>
                            <div class="suggestion-text">厦门有哪些必去的景点？</div>
                        </div>
                        
                        <div class="suggestion-card" onclick="sendSuggestion('厦门的特色美食有哪些？')">
                            <div class="suggestion-icon">🍜</div>
                            <div class="suggestion-title">特色美食</div>
                            <div class="suggestion-text">厦门的特色美食有哪些？</div>
                        </div>
                        
                        <div class="suggestion-card" onclick="sendSuggestion('厦门3天2夜旅游攻略')">
                            <div class="suggestion-icon">📅</div>
                            <div class="suggestion-title">旅游攻略</div>
                            <div class="suggestion-text">厦门3天2夜旅游攻略</div>
                        </div>
                        
                        <div class="suggestion-card" onclick="sendSuggestion('厦门的交通出行方式')">
                            <div class="suggestion-icon">🚌</div>
                            <div class="suggestion-title">交通出行</div>
                            <div class="suggestion-text">厦门的交通出行方式</div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="chat-input-container">
                <div class="chat-input-wrapper">
                    <textarea 
                        id="chatInput" 
                        class="chat-input" 
                        placeholder="请输入您的旅游问题..."
                        rows="1"
                    ></textarea>
                    <button id="sendButton" class="send-button" onclick="sendMessage()">
                        ➤
                    </button>
                </div>
            </div>
        </div>
    </div>

    <script>
        const API_KEY = 'sk-7ef6b65715ff4d4d94648555aa6c1016';
        const API_URL = 'https://api.deepseek.com/v1/chat/completions';
        
        const chatMessages = document.getElementById('chatMessages');
        const chatInput = document.getElementById('chatInput');
        const sendButton = document.getElementById('sendButton');
        
        let isLoading = false;

        // 自动调整输入框高度
        chatInput.addEventListener('input', function() {
            this.style.height = 'auto';
            this.style.height = Math.min(this.scrollHeight, 120) + 'px';
        });

        // 回车发送消息
        chatInput.addEventListener('keydown', function(e) {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                sendMessage();
            }
        });

        function sendSuggestion(text) {
            chatInput.value = text;
            sendMessage();
        }

        function addMessage(content, isUser = false) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user' : 'ai'}`;
            
            messageDiv.innerHTML = `
                <div class="message-avatar">${isUser ? '👤' : '🤖'}</div>
                <div class="message-content">${content}</div>
            `;
            
            chatMessages.appendChild(messageDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        function showLoading() {
            const loadingDiv = document.createElement('div');
            loadingDiv.className = 'message ai';
            loadingDiv.id = 'loadingMessage';
            
            loadingDiv.innerHTML = `
                <div class="message-avatar">🤖</div>
                <div class="message-content">
                    <div class="loading">
                        <span>AI正在思考中</span>
                        <div class="loading-dots">
                            <div class="loading-dot"></div>
                            <div class="loading-dot"></div>
                            <div class="loading-dot"></div>
                        </div>
                    </div>
                </div>
            `;
            
            chatMessages.appendChild(loadingDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        function hideLoading() {
            const loadingMessage = document.getElementById('loadingMessage');
            if (loadingMessage) {
                loadingMessage.remove();
            }
        }

        function showError(message) {
            const errorDiv = document.createElement('div');
            errorDiv.className = 'error-message';
            errorDiv.innerHTML = `❌ ${message}`;
            chatMessages.appendChild(errorDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        async function sendMessage() {
            const message = chatInput.value.trim();
            if (!message || isLoading) return;

            console.log('发送消息:', message); // 调试日志

            // 清除欢迎消息
            const welcomeMessage = document.querySelector('.welcome-message');
            if (welcomeMessage) {
                welcomeMessage.remove();
            }

            // 添加用户消息
            addMessage(message, true);
            chatInput.value = '';
            chatInput.style.height = 'auto';

            // 设置加载状态
            isLoading = true;
            sendButton.disabled = true;
            showLoading();

            try {
                console.log('开始API调用...'); // 调试日志
                
                const response = await fetch(API_URL, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${API_KEY}`
                    },
                    body: JSON.stringify({
                        model: 'deepseek-chat',
                        messages: [
                            {
                                role: 'system',
                                content: '你是一个专业的厦门旅游助手，专门为游客提供厦门旅游相关的咨询服务。请用友好、专业的语气回答用户关于厦门旅游的问题，包括景点推荐、美食介绍、交通指南、住宿建议、旅游攻略等。回答要详细实用，并且要体现厦门的地方特色。'
                            },
                            {
                                role: 'user',
                                content: message
                            }
                        ],
                        temperature: 0.7,
                        max_tokens: 2000
                    })
                });

                console.log('API响应状态:', response.status); // 调试日志
                hideLoading();

                if (!response.ok) {
                    const errorText = await response.text();
                    console.error('API错误响应:', errorText);
                    throw new Error(`API请求失败: ${response.status} - ${errorText}`);
                }

                const data = await response.json();
                console.log('API响应数据:', data); // 调试日志
                
                if (data.choices && data.choices[0] && data.choices[0].message) {
                    const aiResponse = data.choices[0].message.content;
                    addMessage(formatResponse(aiResponse));
                } else {
                    throw new Error('API响应格式错误');
                }

            } catch (error) {
                hideLoading();
                console.error('API调用错误:', error);
                
                // 更详细的错误信息
                if (error.name === 'TypeError' && error.message.includes('fetch')) {
                    showError('网络连接失败，请检查网络连接后重试。');
                } else if (error.message.includes('401')) {
                    showError('API密钥验证失败，请检查配置。');
                } else if (error.message.includes('429')) {
                    showError('请求过于频繁，请稍后再试。');
                } else {
                    showError(`抱歉，AI助手暂时无法回应：${error.message}`);
                }
            } finally {
                isLoading = false;
                sendButton.disabled = false;
            }
        }

        function formatResponse(text) {
            // 简单的文本格式化
            return text
                .replace(/\n\n/g, '<br><br>')
                .replace(/\n/g, '<br>')
                .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
                .replace(/\*(.*?)\*/g, '<em>$1</em>');
        }

        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            chatInput.focus();
            console.log('AI助手页面加载完成'); // 调试日志
        });
    </script>
</body>
</html>
